package cn.itcast.travel.web.servlet;

import cn.itcast.travel.domain.PageBean;
import cn.itcast.travel.domain.Route;
import cn.itcast.travel.domain.User;
import cn.itcast.travel.service.FavoriteService;
import cn.itcast.travel.service.RouteService;
import cn.itcast.travel.service.impl.FavoriteServiceImpl;
import cn.itcast.travel.service.impl.RouteServiceImpl;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * 路线的servlet
 *
 * @author
 * */
@WebServlet("/route/*")
public class RouteServlet extends BaseServlet {
    private RouteService routeService=new RouteServiceImpl();
    private FavoriteService favoriteService=new FavoriteServiceImpl();

    /**分页数据的查询*/
    public void pageQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取导航条的项目位置
        String cidStr = request.getParameter("cid");
        //获取当前页码
        String currentPageStr = request.getParameter("currentPage");
        //获取分页的条数
        String rowsStr = request.getParameter("rows");
        //获取模糊查询的条件
        String rname = request.getParameter("rname");
        //防止rname乱码
        if (!rname.equals("")){
            rname=new String(rname.getBytes("iso-8859-1"),"utf-8");
        }
        int cid=0;
        if (cidStr!=null&&cidStr.length()>0 && !"null".equals(cidStr)){
            cid=Integer.parseInt(cidStr);
        }
        int currentPage=0;
        if (currentPageStr!=null&&currentPageStr.length()>0){
            currentPage=Integer.parseInt(currentPageStr);
        }else{
            //如果没有当前页码，默认为第一页
            currentPage=1;
        }
        int rows=0;
        if (rowsStr!=null&&rowsStr.length()>0){
            rows=Integer.parseInt(rowsStr);
        }else{
            //如果分页条数，默认为5条数据
            rows=5;
        }
        //查询分页数据
        PageBean pageBean = routeService.pageQuery(cid, currentPage, rows,rname);
        writeValue(pageBean,response);
    }

    /**旅游线路的详情展示*/
    public void findOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String rid = request.getParameter("rid");
        Route route=routeService.findOne(Integer.parseInt(rid));
        writeValue(route,response);
    }
    /**判断用户是否收藏该线路*/
    public void isFavorite(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String ridstr = request.getParameter("rid");
        int rid = Integer.parseInt(ridstr);
        User user = (User) request.getSession().getAttribute("login_msg");
        int uid;
        if (user==null){
            //未登录
            uid=0;
        }else{
            //登录了
            uid=user.getUid();
        }
        boolean favorite = favoriteService.isFavorite(rid, uid);
        writeValue(favorite,response);
    }
    /**添加收藏的功能*/
    public void addFavorite(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String ridstr = request.getParameter("rid");
        int rid = Integer.parseInt(ridstr);
        User user = (User) request.getSession().getAttribute("login_msg");
        int uid;
        if (user==null){
            //未登录
            uid=0;
        }else{
            //登录了
            uid=user.getUid();
        }
        favoriteService.addFavorite(rid,uid);
    }
}
